package com.yundong.gongchengji.ui.my.activity;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yundong.gongchengji.R;
import com.yundong.gongchengji.base.BaseActivity;
import com.yundong.gongchengji.util.TimePickerUrl;
import com.yundong.gongchengji.views.NumberPickerView;
/**
 * 添加项目经验
 * Created by ls on 2017/2/21.
 */

public class AddProjectActivity extends BaseActivity implements View.OnClickListener{
    private PopupWindow popupWindow;
    private int year;
    private int month;
    private int day;
    private String[] yearStr;
    private String[] monthStr;
    private String[] dayStr;
    private NumberPickerView picker_years;
    private NumberPickerView picker_month;
    private NumberPickerView picker_day;
    private TimePickerUrl timePickerUrl;

    private View linearlayoutStartTime;
    private View linearlayoutEndTime;
    @Override
    public int getContentViewId() {
        return R.layout.activity_add_project;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setLeft();
        setTitle("添加项目经验");

        linearlayoutStartTime= findViewById(R.id.linearlayout_start_time);
        linearlayoutEndTime=findViewById(R.id.linearlayout_end_time);
    }

    @Override
    protected void initVariable() {
        linearlayoutStartTime.setOnClickListener(this);
        linearlayoutEndTime.setOnClickListener(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    public void showPopu(){
        View view = LayoutInflater.from(this).inflate(R.layout.popu_layout_bottom1, null);
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        //设置显示消失动画
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.4f;
        getWindow().setAttributes(lp);
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp =getWindow().getAttributes();
                lp.alpha = 1.0f;
                getWindow().setAttributes(lp);
            }
        });
        TextView textType= (TextView) view.findViewById(R.id.text_type);
        textType.setVisibility(View.GONE);
        timePickerUrl=new TimePickerUrl(this);
        year =timePickerUrl.getYears(); // 获取Calendar对象中的年
        month = timePickerUrl.getMonth();// 获取Calendar对象中的月
        day = timePickerUrl.getDay();// 获取这个月的第几天

        picker_years = (NumberPickerView) view.findViewById(R.id.picker_years);
        picker_month = (NumberPickerView) view.findViewById(R.id.picker_month);
        picker_day = (NumberPickerView) view.findViewById(R.id.picker_day);

        yearStr = getResources().getStringArray(R.array.years_display);
        monthStr = getResources().getStringArray(R.array.month_display);
        timePickerUrl.setData(picker_years, 0, yearStr.length - 1, timePickerUrl.getWeiZhi(year + "", yearStr));
        timePickerUrl.setData(picker_month, 0, monthStr.length - 1, timePickerUrl.getWeiZhi(month + "", monthStr));
        dayStr= timePickerUrl. changeDay(picker_day,year,month);
        timePickerUrl.setData(picker_day, 0, dayStr.length - 1, timePickerUrl.getWeiZhi(day + "", dayStr));

        picker_years.setOnValueChangedListener(new NumberPickerView.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPickerView picker, int oldVal, int newVal) {
                year = Integer.parseInt(yearStr[newVal]);
                timePickerUrl. changeDay(picker_day,year,month);
            }
        });
        picker_month.setOnValueChangedListener(new NumberPickerView.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPickerView picker, int oldVal, int newVal) {
                month = Integer.parseInt(monthStr[newVal]);
                timePickerUrl. changeDay(picker_day,year,month);
            }
        });
        TextView textCanel= (TextView) view.findViewById(R.id.text_canel);
        TextView textOk= (TextView) view.findViewById(R.id.text_ok);
        textCanel.setOnClickListener(this);
        textOk.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.text_ok:
                showToast(this,yearStr[picker_years.getValue()]+"="+monthStr[picker_month.getValue()]+"="+dayStr[picker_day.getValue()]);
                popupWindow.dismiss();
                break;
            case R.id.text_canel:
                popupWindow.dismiss();
                break;
            case R.id.linearlayout_start_time:
                showPopu();
                break;
            case R.id.linearlayout_end_time:
                showPopu();
                break;
        }
    }
}
